import blue
import trinity
import svc

class EveDeviceMgr(svc.device):
    __guid__ = 'svc.eveDevice'
    __replaceservice__ = 'device'

    def AppRun(self):
        if not settings.public.generic.Get('resourceUnloading', 1):
            trinity.SetEveSpaceObjectResourceUnloadingEnabled(0)
        self.defaultPresentationInterval = self.d3dconst.D3DPRESENT_INTERVAL_ONE



    def GetShaderModel(self, val):
        if (val == 3):
            shaderModel = trinity.GetMaxShaderModelSupported()
            if prefs.GetValue('depthEffectsEnabled', self.GetDefaultDepthEffectsEnabled()):
                if (shaderModel == 'SM_3_0_HI'):
                    shaderModel = 'SM_3_0_DEPTH'
            return shaderModel
        else:
            return 'SM_2_0_LO'



    def GetWindowModes(self):
        self.LogInfo('GetWindowModes')
        adapter = self.CurrentAdapter()
        if (self.GetFormatNameByConst(adapter['Format']) not in self.formatTable):
            return [(mls.UI_SHARED_FULLSCREEN, 0)]
        else:
            if blue.win32.IsTransgaming():
                return [(mls.UI_SHARED_WINDOWMODE, 1), (mls.UI_SHARED_FULLSCREEN, 0)]
            return [(mls.UI_SHARED_WINDOWMODE, 1),
             (mls.UI_SHARED_FULLSCREEN, 0),
             (mls.UI_SHARED_FIXEDWINDOW, 2)]



    def GetAppShaderModel(self):
        shaderQuality = prefs.GetValue('shaderQuality', self.GetDefaultShaderQuality())
        return self.GetShaderModel(shaderQuality)



    def GetAppSettings(self):
        settings = {}
        lodQuality = prefs.GetValue('lodQuality', self.GetDefaultLodQuality())
        if (lodQuality == 1):
            settings = {'eveSpaceSceneVisibilityThreshold': 15.0,
             'eveSpaceSceneLowDetailThreshold': 140.0,
             'eveSpaceSceneMediumDetailThreshold': 480.0}
        elif (lodQuality == 2):
            settings = {'eveSpaceSceneVisibilityThreshold': 6,
             'eveSpaceSceneLowDetailThreshold': 70,
             'eveSpaceSceneMediumDetailThreshold': 240}
        elif (lodQuality == 3):
            settings = {'eveSpaceSceneVisibilityThreshold': 3.0,
             'eveSpaceSceneLowDetailThreshold': 35.0,
             'eveSpaceSceneMediumDetailThreshold': 120.0}
        return settings



    def GetAppMipLevelSkipExclusionDirectories(self):
        return ['res:/Texture/IntroScene', 'res:/UI/Texture']



    def GetAppScene(self):
        return 'res:/Scene/emptyscene.red'



    def IsWindowed(self, settings = None):
        if (settings is None):
            settings = self.GetSettings()
        if blue.win32.IsTransgaming():
            return (not sm.GetService('cider').GetFullscreen())
        else:
            return settings.Windowed



    def SetDevice(self, device, tryAgain = 1, fallback = 0, keepSettings = 1, hideTitle = None, userModified = False, muteExceptions = False, updateWindowPosition = True):
        svc.device.SetDevice(self, device, tryAgain, fallback, keepSettings, hideTitle, userModified, muteExceptions, updateWindowPosition)
        sm.GetService('cider').SetFullscreen(sm.GetService('cider').GetFullscreen())



    def ToggleWindowed(self, *args):
        if blue.win32.IsTransgaming():
            cider = sm.GetService('cider')
            cider.SetFullscreen((not cider.GetFullscreen()))
        else:
            svc.device.ToggleWindowed(self, args)



    def GetDefaultFastCharacterCreation(self):
        return 0



    def GetDefaultClothSimEnabled(self):
        shaderModel = trinity.GetShaderModel()
        if shaderModel.startswith('SM_3'):
            return 1
        else:
            return 0



    def GetDefaultCharTextureQuality(self):
        return 1



    def EnforceDeviceSettings(self, settings):
        settings.BackBufferFormat = self.GetBackbufferFormats()[0][1]
        settings.AutoDepthStencilFormat = self.GetStencilFormats()[0][1]
        return settings



    def SupportsDepthEffects(self):
        return trinity.renderJobUtils.DeviceSupportsIntZ()



    def GetDefaultDepthEffectsEnabled(self):
        return (trinity.renderJobUtils.DeviceSupportsIntZ() and (not trinity.GetMaxShaderModelSupported().startswith('SM_2')))



    def GetAdapterResolutionsAndRefreshRates(self, set = None):
        (options, resoptions,) = svc.device.GetAdapterResolutionsAndRefreshRates(self, set)
        if set.Windowed:
            maxWidth = uicore.triapp.GetVirtualScreenWidth()
            maxHeight = uicore.triapp.GetVirtualScreenHeight()
            maxOp = (('%sx%s' % (maxWidth, maxHeight)), (maxWidth, maxHeight))
            if (maxOp not in options):
                options.append(maxOp)
        return (options, resoptions)



    def CheckIfLowEndGraphicsDevice(self):
        lowEndDevices = {(4098, 29125): ('ATI / AMD', 'M56', 'ATI MOBILITY /ATI RADEON X1600'),
         (4098, 31007): ('ATI / AMD', 'RS690M', 'ATI Mobility Radeon x1100'),
         (4098, 29312): ('ATI / AMD', 'R580', 'ATI RADEON X1950 Series'),
         (4098, 29122): ('ATI / AMD', 'RV530', 'ATI RADEON X1600 Series'),
         (4098, 28997): ('ATI / AMD', 'M54', 'ATI MOBILITY /ATI RADEON X1400'),
         (4098, 29059): ('ATI / AMD', 'RV515', 'ATI RADEON X1300/X1550 Series'),
         (4098, 29257): ('ATI / AMD', 'R580', 'ATI RADEON X1900 Series'),
         (4098, 23392): ('ATI / AMD', 'RV370', 'ATI RADEON X300/X550/X1050 Series'),
         (4098, 29121): ('ATI / AMD', 'RV535', 'ATI RADEON X1650 Series'),
         (4098, 29248): ('ATI / AMD', 'R580', 'ATI RADEON X1950 Series'),
         (4098, 29127): ('ATI / AMD', 'RV535', 'ATI RADEON X1650 Series'),
         (4098, 29126): ('ATI / AMD', 'RV530', 'ATI RADEON X1650 Series'),
         (4098, 29320): ('ATI / AMD', 'R580', 'ATI RADEON X1950 GT'),
         (4098, 28998): ('ATI / AMD', 'RV515', 'ATI RADEON X1300 / X1550 Series'),
         (4098, 16723): ('ATI / AMD', 'RV350', 'ATI RADEON 9550/X1050 Series'),
         (4098, 28994): ('ATI / AMD', 'RV515', 'ATI RADEON X1300/X1550 Series'),
         (4098, 29001): ('ATI / AMD', 'M52', 'ATI MOBILITY /ATI RADEON X1300'),
         (4098, 29252): ('ATI / AMD', 'R580', 'ATI RADEON X1950 Series'),
         (4098, 31006): ('ATI / AMD', 'RS690', 'ATI RADEON X1200 Series'),
         (4098, 29063): ('ATI / AMD', 'RV515', 'ATI RADEON X1300/X1550 Series'),
         (4098, 23395): ('ATI / AMD', 'REV00', 'ATI RADEON X300/X550/X1050 Series'),
         (4098, 28928): ('ATI / AMD', 'R520', 'ATI RADEON X1800 Series'),
         (4098, 29120): ('ATI / AMD', 'RV530', 'ATI RADEON X1600 Series'),
         (4098, 29057): ('ATI / AMD', 'RV515', 'ATI RADEON X1600 Series'),
         (4098, 29141): ('ATI / AMD', 'M56', 'ATI MOBILITY /ATI RADEON X1700'),
         (4098, 29329): ('ATI / AMD', 'R580', 'ATI RADEON X1650 Series'),
         (4098, 29259): ('ATI / AMD', 'R580', 'ATI RADEON X1900 Series'),
         (4098, 29123): ('ATI / AMD', 'RV535', 'ATI RADEON X1650 Series'),
         (4098, 28995): ('ATI / AMD', 'RV515', 'ATI RADEON X1550 Series'),
         (4098, 28937): ('ATI / AMD', 'R520', 'ATI RADEON X1800 Series'),
         (4098, 29078): ('ATI / AMD', 'M52', 'ATI MOBILITY /ATI RADEON X1350'),
         (4098, 29066): ('ATI / AMD', 'M54', 'ATI MOBILITY /ATI RADEON X2300'),
         (4098, 29124): ('ATI / AMD', 'M56GL', 'ATI MOBILITY FireGL V5200'),
         (4098, 29140): ('ATI / AMD', 'M56GL', 'ATI MOBILITY FireGL V5250'),
         (4318, 977): ('NVIDIA', 'C61', 'NVIDIA GeForce 6100 nForce 405'),
         (4318, 578): ('NVIDIA', 'C51', 'NVIDIA GeForce 6100'),
         (4318, 583): ('NVIDIA', 'C51, Geforce 6100 Go'),
         (4318, 978): ('NVIDIA', 'C61', 'NVIDIA GeForce 6100 nForce 400'),
         (4318, 976): ('NVIDIA', 'C61', 'NVIDIA GeForce 6150SE nForce 430'),
         (4318, 577): ('NVIDIA', 'C51', 'NVIDIA GeForce 6150 LE'),
         (4318, 576): ('NVIDIA', 'C51', 'NVIDIA GeForce 6150'),
         (4318, 545): ('NVIDIA', 'NV44', 'NVIDIA GeForce 6200'),
         (4318, 353): ('NVIDIA', 'NV44, GeForce 6200 TurboCache'),
         (4318, 335): ('NVIDIA', 'NV43', 'NVIDIA GeForce 6200'),
         (4318, 355): ('NVIDIA', 'NV44', 'NVIDIA GeForce 6200 LE'),
         (4318, 546): ('NVIDIA', 'NV44', 'NVIDIA GeForce 6200 A-LE'),
         (4318, 354): ('NVIDIA', 'NV44', 'NVIDIA GeForce 6200SE TurboCache(TM)'),
         (4318, 243): ('NVIDIA', 'BR02', 'NVIDIA GeForce 6200'),
         (4318, 359): ('NVIDIA', 'NV44', 'NVIDIA GeForce Go 6200'),
         (4318, 326): ('NVIDIA', 'NV43', 'NVIDIA GeForce Go 6200 TE/6600 TE'),
         (4318, 360): ('NVIDIA', 'NV44', 'NVIDIA GeForce Go 6400'),
         (4318, 352): ('NVIDIA', 'NV44', 'NVIDIA GeForce 6500'),
         (4318, 321): ('NVIDIA', 'NV43', 'NVIDIA GeForce 6600 PCI-E Video Adapter'),
         (4318, 320): ('NVIDIA', 'NV43', 'NVIDIA GeForce 6600 GT'),
         (4318, 242): ('NVIDIA', 'BR02', 'NVIDIA GeForce 6600'),
         (4318, 241): ('NVIDIA', 'BR02', 'NVIDIA GeForce 6600 GT'),
         (4318, 322): ('NVIDIA', 'NV43', 'NVIDIA GeForce 6600 LE'),
         (4318, 328): ('NVIDIA', 'NV43', 'NVIDIA GeForce Go 6600'),
         (4318, 244): ('NVIDIA', 'BR02', 'NVIDIA GeForce 6600 LE'),
         (4318, 193): ('NVIDIA', 'NV41', 'NVIDIA GeForce 6800'),
         (4318, 69): ('NVIDIA', 'NV40', 'NVIDIA GeForce 6800 GT'),
         (4318, 192): ('NVIDIA', 'NV41', 'NVIDIA GeForce 6800 GS'),
         (4318, 249): ('NVIDIA', 'BR02', 'NVIDIA GeForce 6800 Series GPU'),
         (4318, 2017): ('NVIDIA', 'C73', 'NVIDIA GeForce 7100 / NVIDIA nForce 630i'),
         (4318, 915): ('NVIDIA', 'G73', 'NVIDIA GeForce 7300 GT'),
         (4318, 465): ('NVIDIA', 'G72', 'NVIDIA GeForce 7300 LE'),
         (4318, 479): ('NVIDIA', 'G72', 'NVIDIA GeForce 7300 GS'),
         (4318, 467): ('NVIDIA', 'G7200', 'NVIDIA GeForce 7300 SE/7200 GS'),
         (4318, 1331): ('NVIDIA', '', 'NVIDIA GeForce 7000M'),
         (4318, 738): ('NVIDIA', 'BR02', 'NVIDIA GeForce 7300 GT'),
         (4318, 2019): ('NVIDIA', 'C73', 'NVIDIA GeForce 7050')}
        identifier = trinity.d3d.GetAdapterIdentifier()
        if (identifier is None):
            return False
        else:
            deviceID = identifier.get('DeviceId', None)
            vendorID = identifier.get('VendorId', None)
            return ((vendorID, deviceID) in lowEndDevices)




